#include "StdAfx.h"
#include "WMPUIAddons.h"
#include "..\WMP\Common.h"
#include "WMPUtils.h"
#include "WMPPlus.h"

#pragma unmanaged

WMPUIAddons::WMPUIAddons(WMPPlusPublic* owner) {
	this->Owner = owner;
}

bool WMPUIAddons::ProcessKey(HWND hWnd, WORD code, DWORD info) {
	if (!Core->getBoolProp(WMPPLUS_PROPERTY_SETTINGS_FINDASYOUTYPE) ||
		Owner->getWMPlayerWindow() != GetActiveWindow() || IsEditBox(hWnd))
		return false;
	HWND hWMP;
	if (!(hWMP = FindWindowEx(Owner->getWMPlayerWindow(), NULL, _T("WMPAppHost"), NULL)) ||
		!(hWMP = FindWindowEx(hWMP, NULL, _T("WMP Skin Host"), NULL)) ||
		!(hWMP = FindWindowEx(hWMP, NULL, NULL, _T("LibraryContainer"))) ||
		!IsWindowVisible(hWMP) ||
		!FindWindowEx(hWMP, NULL, _T("SysListView32"), _T("PrimaryListView")) ||
		!(hWMP = FindWindowEx(hWMP, NULL, _T("Edit"), NULL)) ||
		!IsWindowEnabled(hWMP))
		return false;
	if (code == VK_ESCAPE) {
		SetFocus(hWMP);
		SendMessage(hWMP, WM_KEYDOWN, VK_ESCAPE, 0);
		SendMessage(hWMP, WM_KEYUP, VK_ESCAPE, 0);
		SetFocus(hWnd);
		return true;
	} else if (code >= 0x21) {
		SetFocus(hWMP);
		PostMessage(hWMP, WM_CHAR, code, info);
		PostMessage(hWMP, EM_SETSEL, -1, 0);
		return true;
	}
	return false;
}

void WMPUIAddons::FixListViewGroupViewMessage(WPARAM &fEnable) {
	if (getDisableGroupHeaders())
		fEnable = FALSE;
}

void WMPUIAddons::UpdateListView(BOOL groupHeaders) {
	HWND hWnd = FindWMPLibraryListWindow(Owner->getWMPlayerWindow());
	if (!hWnd)
		return;
	LVGROUP info;
	memset(&info, 0, sizeof(LVGROUP));
	info.cbSize = sizeof(LVGROUP);
	info.mask = LVGF_STATE;
	if (ListView_GetGroupInfo(hWnd, 0, &info) == -1 || (info.state & LVGS_HIDDEN))
		return;
	ListView_EnableGroupView(hWnd, (WPARAM)groupHeaders);
}

void WINAPI UpdateListViewGroupHeaders(BOOL groupHeaders) {
	if (Plugin && Plugin->Addons)
		Plugin->Addons->UpdateListView(groupHeaders);
}

int WMPUIAddons::Place(HMENU hMenu) {
	int pos = FindMenuItem(hMenu, 18825);
	if (pos == -1 || FindMenuItem(hMenu, 18842) != -1) return -1;
	int place = -1;
	MENUITEMINFO info;
	while (pos >= 0) {
		memset(&info, 0, sizeof(MENUITEMINFO));
		info.cbSize = sizeof(MENUITEMINFO);
		info.fMask = MIIM_SUBMENU | MIIM_DATA;
		GetMenuItemInfo(hMenu, pos, true, &info);
		if (info.hSubMenu) {
			place = pos+1;
			break;
		}
		pos--;
	}
	if (place == -1)
		place = GetMenuItemCount(hMenu);
	return place;
}

int WINAPI PlaceWMPUIAddons(HMENU hMenu) {
	if (Plugin && Plugin->Addons)
		return Plugin->Addons->Place(hMenu);
	return -1;
}

bool WMPUIAddons::ProcessSetTextMessage(LPCTSTR text, LRESULT &res) {
	LPCTSTR title = getTitle();
	if (title && lstrcmp(title, text) != 0) {
		res = FALSE;
		return false;
	}
	return true;
}

void WMPUIAddons::setTitle(LPCTSTR title) {
	if (title && !*title) title = NULL;
	if (lstrcmp(title, getTitle()) == 0)
		return;
	Core->setStrProp(WMPPLUS_PROPERTY_ADDONS_TITLE, title);
	Owner->UpdateTitle(!title ? getWMPTitle() : title);
}

void WINAPI SetWMPlayerTitle(LPCTSTR title) {
	if (Plugin && Plugin->Addons)
		Plugin->Addons->setTitle(title);
}

void WMPUIAddons::SetWindowTitle(HWND hWnd) {
	LPCTSTR title = getTitle();
	if (title)
		SetWindowText(hWnd, title);
}

void GetMenuItemLabel(HMENU hMenu, UINT item, BOOL byPos, LPTSTR label, int clabel) {
	MENUITEMINFO info;
	memset(&info, 0, sizeof(MENUITEMINFO));
	info.cbSize = sizeof(MENUITEMINFO);
	info.fMask = MIIM_TYPE;
	info.dwTypeData = label;
	info.cch = clabel;
	if (!GetMenuItemInfo(hMenu, item, byPos, &info) || info.fType != MFT_STRING)
		*label = 0;
}

void WMPUIAddons::Initialize() {
	CreateTextBuffer(textBuffer);

	setContextMenu(NULL);
	HMODULE loc = LoadLibraryEx(_T("wmploc.dll"), NULL, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
	if (loc) {
		try {
			setContextMenu(LoadMenu(loc, (LPCTSTR)7228));
		} finally (
			FreeLibrary(loc);
		)
	}
	setTagEditor(false);
	if (getContextMenu()) {
		GetMenuItemLabel(getContextMenu(), 7304, false, textBuffer, textBufferLength);
		setTagEditorLabel(textBuffer);
		setTagEditor(!!*textBuffer);
	}
	SendMessage(Plugin->getWMPlayerWindow(), WM_GETTEXT, textBufferLength, (LPARAM)textBuffer);
	setWMPTitle(textBuffer);
	setTitle(NULL);
}

void WMPUIAddons::Dispose() {
	if (getContextMenu())
		DestroyMenu(getContextMenu());
	setContextMenu(NULL);
	setTagEditorLabel(NULL);
	setWMPTitle(NULL);
}

void WINAPI WMPUIAddonsInitialize() {
	if (Plugin && Plugin->Addons)
		Plugin->Addons->Initialize();
}

void WINAPI WMPUIAddonsDispose() {
	if (Plugin && Plugin->Addons)
		Plugin->Addons->Dispose();
}
